package com.example.cms2.Controller;

import com.example.cms2.Mapper.EmployeeMapper;
import com.example.cms2.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;

/**
 * @return
 */
@Controller
public class EmployeeController {
    @Autowired
    EmployeeMapper employeeMapper;
    @RequestMapping("/emps")
    public String list(Model model){
        List<Employee> employees = employeeMapper.getAll();//查询全部
        model.addAttribute("emps",employees);
        return "emp/list";
    }
    @GetMapping("/emp")
    public String toAddpage(){
        return "emp/add";
    }
    @PostMapping("/emp")
    public String addEmp(Employee employee){
        employeeMapper.insertEmp(employee);//添加
        return "redirect:/emps";
    }
    //修改页面 先查后改 就是修改
    @RequestMapping("/empup")
    public String toUpdateEmp(Integer id, Model model){
        //先查出id的员工
        Employee empByid = employeeMapper.getEmpById(id);
        model.addAttribute("emp",empByid);
        return "emp/update";
    }
    @PostMapping("/updateEmp")
    public String updateEmp(Employee employee){
        employeeMapper.updateEmpById(employee);
        return "redirect:/emps";
    }
    @RequestMapping("/deleteEmp")
    public String toDeleteEmp(Integer id){
        employeeMapper.deleteEmpById(id);
        return "redirect:/emps";
    }
}
